package cn.edu.hncst.dao.impl;

import cn.edu.hncst.dao.UserDao;
import cn.edu.hncst.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;;import java.util.List;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
    /**
     * 分页查询
     * @param startCount 开始
     * @param pageSize 大小
     * @return
     */
    @Override
    public List<User> pageQuery(int startCount, int pageSize) {
        String sql = "SELECT * FROM user LIMIT ?,?";
        return template.query(sql, new BeanPropertyRowMapper<>(User.class), startCount, pageSize);
    }

    @Override
    public User findById(String username, String password) {
        String sql = "SELECT * FROM user WHERE username = ? and password = ?";
        try{
            User query = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username, password);
            return query;
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public List<User> searchUser(String keywords, int startCount, int pageSize) {
        String sql = "SELECT * FROM user WHERE name like ? or gender=? or address like ? or qq=? or email=? LIMIT ?,?";

        try{
            List<User> query = template.query(sql, new Object[]{ "%"+keywords+"%", keywords, "%"+keywords+"%", keywords, keywords,startCount,pageSize}, new BeanPropertyRowMapper<>(User.class));
            return query;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public int querySearchCount(String keywords) {
        String sql = "SELECT COUNT(*) FROM user WHERE name like ? or gender=? or address like ?  or qq=? or email=?";
        try{
            Integer integer = template.queryForObject(sql, new Object[]{ "%" + keywords + "%", keywords, "%" + keywords + "%", keywords, keywords}, Integer.class);
            return integer;
        }catch (Exception e){
            return 0;
        }
    }


}
